<?php
/**
 * PHP Class
 *
 * LICENSE
 *
 * Processing 	Delete Recode
 *
 * @category   	PHP classes
 * @package    	Database
 * @copyright  	Copyright (c) 2007-2012 Professional World company(http://www.worldprovn.com/)
 *									   & Vietnam Zend Framework community (http://www.zend.vn/)
 * @author		DungDam (quangdungninh@worldprovn.com)
 * @license    	GNU General Public License Version 2 or later (the "GPL")
 * @version     v1.0 08:00:00 PM Jan 29, 2013
 */
class Zendvn_Model_DeleteRecode{
	
	public function user($arrParam = null,$options = null, $db){
		$config 		= new Zendvn_Controller_Config(array('module'=>$arrParam['module'],'fileConfig'=>'user.ini'));
		$file 			= new Zendvn_File_Upload();
		$avatarConfig 	= $config->getConfig('config');
		
		if($options == null){
			if((int)$arrParam['id']>0){
				$itemInfo = $db->getItem(array('id'=>$arrParam['id']));
				if($itemInfo['group_default'] != 1 && $itemInfo['type'] != 'root'){
					
					$file->remove($avatarConfig['uploadDir'],$itemInfo['avatar']);
					$file->remove($avatarConfig['thumbDir'],$itemInfo['avatar']);
					$file->remove($avatarConfig['thumbDir1'],$itemInfo['avatar']);
					$file->remove($avatarConfig['thumbDir2'],$itemInfo['avatar']);
					$file->remove($avatarConfig['thumbDir3'],$itemInfo['avatar']);
		
					$where = 'id = ' . (int)$arrParam['id'];
					$db->delete($where);
				}
			}
		}
		if($options['task'] == 'multi'){
			if(count($arrParam['cid'])>0){
				foreach ($arrParam['cid'] as $val){
					$itemInfo = $db->getItem(array('id'=>$val));
					if($itemInfo['group_default'] != 1 && $itemInfo['type'] != 'root'){
		
						$file->remove($avatarConfig['uploadDir'],$itemInfo['avatar']);
						$file->remove($avatarConfig['thumbDir'],$itemInfo['avatar']);
						$file->remove($avatarConfig['thumbDir1'],$itemInfo['avatar']);
						$file->remove($avatarConfig['thumbDir2'],$itemInfo['avatar']);
						$file->remove($avatarConfig['thumbDir3'],$itemInfo['avatar']);
		
						$where = 'id = ' . (int)$val;
						$db->delete($where);
					}
				}
			}
		}
	}
	public function group($arrParam = null,$options = null, $db){
		if($options == null){
			if((int)$arrParam['id']>0){
				$itemInfo = $db->getItem(array('id'=>$arrParam['id']));
				if($itemInfo['group_default'] != 1 && $itemInfo['type'] != 'root'){
					$config = new Zendvn_Controller_Config(array('module'=>$arrParam['module'],
							'fileConfig'=>'user-group.ini'));
					$db->removeNode($arrParam['id'], 'branch');
						
					$file = new Zendvn_File_Upload();
					$avatarConfig = $config->getConfig('avatar');
					$file->remove($avatarConfig['uploadDir'],$itemInfo['avatar']);
					$file->remove($avatarConfig['thumbDir'],$itemInfo['avatar']);
		
					$rankingConfig = $config->getConfig('ranking');
					$file->remove($rankingConfig['uploadDir'],$itemInfo['ranking']);
				}
			}
		}
		if($options['task'] == 'multi'){
			if(count($arrParam['cid'])>0){
				$config = new Zendvn_Controller_Config(array('module'=>$arrParam['module'],
						'fileConfig'=>'user-group.ini'));
		
				$file = new Zendvn_File_Upload();
				foreach ($arrParam['cid'] as $val){
					$itemInfo = $db->getItem(array('id'=>$val));
					if($itemInfo['group_default'] != 1 && $itemInfo['type'] != 'root'){
		
						$avatarConfig = $config->getConfig('avatar');
						$file->remove($avatarConfig['uploadDir'],$itemInfo['avatar']);
						$file->remove($avatarConfig['thumbDir'],$itemInfo['avatar']);
		
						$rankingConfig = $config->getConfig('ranking');
						$file->remove($rankingConfig['uploadDir'],$itemInfo['ranking']);
							
						$db->removeNode((int)$val, 'branch');
					}
				}
			}
		}
	}
	public function newsCategory($arrParam = null,$options = null, $db){
		if($options == null){
			if((int)$arrParam['id']>0){
				$children = $db->getChildrenNode($arrParam['id'],array('task'=>'remove-image','node'=>true));
				if(!empty($children)){	
					$config = new Zendvn_Controller_Config(array('module'=>$arrParam['module'],
													 	  		'fileConfig'=>'news-category.ini'));
					
					$file = new Zendvn_File_Upload();
					$imageConfig = $config->getConfig('category');
					foreach ($children as $images){
						$file->remove($imageConfig['uploadDir'],$images['image']);
						$file->remove($imageConfig['thumbDir'],$images['image']);
						
					}
					$db->removeNode($arrParam['id'], 'branch');
				}
			}
		}
		if($options['task'] == 'multi'){		
			if(count($arrParam['cid'])>0){	
				foreach ($arrParam['cid'] as $val){
					$children = $db->getChildrenNode($val,array('task'=>'remove-image','node'=>true));
					if(!empty($children)){	
						$config = new Zendvn_Controller_Config(array('module'=>$arrParam['module'],
														 	  		'fileConfig'=>'news-category.ini'));
						
						$file = new Zendvn_File_Upload();
						$imageConfig = $config->getConfig('category');
						foreach ($children as $images){
							$file->remove($imageConfig['uploadDir'],$images['image']);
							$file->remove($imageConfig['thumbDir'],$images['image']);
							
						}
						$db->removeNode($val, 'branch');
					}
				}
			}
		}
	}
	public function newsItem($arrParam = null,$options = null, $db){
		$config = new Zendvn_Controller_Config(array('module'=>$arrParam['module'],'fileConfig'=>'news-category.ini'));
		$file = new Zendvn_File_Upload();
		if($options == null){
			if((int)$arrParam['id']>0){
				$itemInfo = $db->getItem(array('id'=>$arrParam['id']));
				$avatarConfig = $config->getConfig('item');
				$file->remove($avatarConfig['uploadDir'],$itemInfo['picture']);
				$file->remove($avatarConfig['thumbDir1'],$itemInfo['picture']);
				$file->remove($avatarConfig['thumbDir2'],$itemInfo['picture']);
				$file->remove($avatarConfig['thumbDir3'],$itemInfo['picture']);
				$file->remove($avatarConfig['fileAttachDir'],$itemInfo['file_attach']);
		
				$where = 'id = ' . (int)$arrParam['id'];
				$db->delete($where);
			}
		}
		if($options['task'] == 'multi'){
			if(count($arrParam['cid'])>0){
				
				foreach ($arrParam['cid'] as $val){
					$itemInfo = $db->getItem(array('id'=>$val));
					$avatarConfig = $config->getConfig('item');
					$file->remove($avatarConfig['uploadDir'],$itemInfo['picture']);
					$file->remove($avatarConfig['thumbDir1'],$itemInfo['picture']);
					$file->remove($avatarConfig['thumbDir2'],$itemInfo['picture']);
					$file->remove($avatarConfig['thumbDir3'],$itemInfo['picture']);
					$file->remove($avatarConfig['fileAttachDir'],$itemInfo['file_attach']);
					$where = 'id = ' . (int)$val;
					$db->delete($where);
				}
			}
		}
	}
}